import { Sequelize } from 'sequelize';
import config from 'config';

let sequelize;

export function initialize() {
  const options = config.get('sequelize');
  // const {
  //   host, port, user, password, database
  // } = options;
  // const connection = await mysql.createConnection({
  //   host, port, user, password
  // });
  // // create db if it doesn't already exist
  // await connection.query(`create database if not exists ${database}
  // default character set utf8mb4 collate utf8mb4_0900_ai_ci;`);

  // connect to db
  sequelize = new Sequelize(options);

  return sequelize;
}

// console.log(config.get('sequelize'));
/**
 * 获取Sequelize实例
 * @returns {Sequelize} Sequelize实例
 */
export function getSequelize() {
  if (!sequelize) {
    initialize();
  }
  return sequelize;
}
